import http from '../utils/request'

// 灰度发布相关类型定义
export interface GrayReleaseBinding {
  userId: string
  version: string
}

export interface CreateGrayReleaseDto {
  appId: string
  userId: string
  version: string
  ttl?: number
}

export interface BatchBindGrayReleaseDto {
  appId: string
  userIds: string[]
  version: string
  ttl?: number
}

export interface QueryGrayReleaseDto {
  appId: string
  userId?: string
}

export interface UpdateGrayReleaseDto {
  appId: string
  userId: string
  version: string
  ttl?: number
}

export interface DeleteGrayReleaseDto {
  appId: string
  userId: string
}

export interface BatchDeleteGrayReleaseDto {
  appId: string
  userIds: string[]
}

export interface GrayReleaseQueryResponse {
  appId: string
  total?: number
  bindings?: GrayReleaseBinding[]
  userId?: string
  version?: string
}

export interface VersionStats {
  appId: string
  totalVersions: number
  totalUsers: number
  stats: Array<{
    version: string
    userCount: number
    users: string[]
  }>
}

// 灰度发布 API
export const grayReleaseApi = {
  /**
   * 创建灰度白名单绑定
   */
  createGrayRelease: (data: CreateGrayReleaseDto) => {
    return http.post('/gray-release', data)
  },

  /**
   * 批量绑定灰度白名单
   */
  batchBindGrayRelease: (data: BatchBindGrayReleaseDto) => {
    return http.post('/gray-release/batch-bind', data)
  },

  /**
   * 查询灰度白名单
   */
  queryGrayRelease: (params: QueryGrayReleaseDto) => {
    return http.get('/gray-release', params)
  },

  /**
   * 更新灰度白名单
   */
  updateGrayRelease: (data: UpdateGrayReleaseDto) => {
    return http.put('/gray-release', data)
  },

  /**
   * 删除灰度白名单
   */
  deleteGrayRelease: (data: DeleteGrayReleaseDto) => {
    // DELETE 请求使用 body 传递数据
    return http.delete('/gray-release', data, false, true, '', true)
  },

  /**
   * 批量删除灰度白名单
   */
  batchDeleteGrayRelease: (data: BatchDeleteGrayReleaseDto) => {
    // DELETE 请求使用 body 传递数据
    return http.delete('/gray-release/batch-delete', data, false, true, '', true)
  },

  /**
   * 清理应用的所有灰度白名单
   */
  clearAppGrayRelease: (appId: string) => {
    return http.delete(`/gray-release/clear/${appId}`)
  },

  /**
   * 按版本查询用户列表
   */
  getUsersByVersion: (appId: string, version: string) => {
    return http.get(`/gray-release/version-users/${appId}/${version}`)
  },

  /**
   * 获取版本统计信息
   */
  getVersionStats: (appId: string) => {
    return http.get(`/gray-release/version-stats/${appId}`)
  },

  /**
   * 查询单个用户的灰度版本（公开接口）
   */
  getUserGrayVersion: (appId: string, userId: string) => {
    return http.get(`/gray-release/user-version/${appId}/${userId}`)
  }
}

